// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var css_detail = require("./accountDetail.css");
	var databind = require("databind");
	var dialog = require("dialog");
	var html_verify = require("../apply/apply_verify.html");
	var css_dialog = require("css_dialog");
	var html_coldStorage = require("./html_coldStorage.html");
	var html_showSample = require("./html_showSample.html");
	var html_cancelApplication = require("./html_cancelApplication.html");
	var html_bailReturn = require("./html_bailReturn.html");
	var financeId;
	var productId;
	var productType;	
	var financeNumber;
	var financeState;
	var financeNo;
	var financeAmountt;
	var type;
	var enterpriseId;
	var enterpriseName;
	var financeNo;
	var financeAmountt;
	var tools = require("tools");
	var storage = window.sessionStorage;
	var init = function(page) {
		financeId = page.params.financeId;
		type = page.params.type;
		storage.setItem("financeId", financeId);
		inquireFinanceApplicationDetail();
		$(".d2 a").removeClass("active_tab");
		$(".accountDetail").addClass("active_tab");
		$(".accountDetail").parent().parent().addClass("active_tab1");
		$(".accountDetail").parent().parent().removeClass("active_tab2");
		$(".accountDetail").parent().parent().removeClass("active_tab3");
		$(".accountDetail").parent().parent().removeClass("active_tab4");
		router.init();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}

		//融资证明材料
		$(".accountMaterial").unbind("click").click(function() {
			router.route("account/accountDetailMaterial.html?financeId=" + financeId + "&type="+type);
		});

		//融资动态
		$(".accountProcess").unbind("click").click(function() {
			router.route("account/accountDetailProcess.html?financeId=" + financeId + "&type="+type+ "&enterpriseId=" +enterpriseId +"&enterpriseName="+enterpriseName+"&productType="+productType);
		});

		//提货列表
		$(".warehousePickups").unbind("click").click(function() {
			router.route("account/warehousePickupList2.html?financeId=" + financeId + "&type="+type);
		});

	};
	
	//提交融资申请
	var submitFinanceApplication = function() {
		base.fnAjax({
			action: "submitFinanceApplication.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					router.route("account/accountMain.html");
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};

	//查询评价信息
	var inquireFinanceEvaluate = function() {
		base.fnAjax({
			action: "inquireFinanceEvaluate.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas[0].content == "") {
						$(".buttonOperate").html("评价").attr("id", "evalute");
					} else {
						$(".buttonOperate").remove();
					}
				} else {
					$(".buttonOperate").html("评价").attr("id", "evalute");
					//                  base.alert(2,"信息获取失败");
				}
			}
		});
	};

	//修改融资申请
	var updateFinanceApplication = function() {
		location.href = "../apply/apply_main.html?financeId=" + financeId + "&productType=" + productType;
	}

	//提货申请
	var pickupFinanceApplication = function() {
		router.route("account/accountPickup.html?financeId=" + financeId + "&financeNumber=" + financeNumber);
	}

	//融资评价
	var evaluteFinanceApplication = function() {
		router.route("account/accountEvaluate.html?financeId=" + financeId);
	}

	//赎货申请
	var ransomApplication = function() {
		router.route("account/accountRansomPickup.html?financeId=" + financeId + "&financeNumber=" + financeNumber);
	}

	//提前还款
	var preApplication = function() {
		router.route("account/accountPrePickup.html?financeId=" + financeId + "&financeNumber=" + financeNumber);
	}

	//延期计息
	var interestDelayApplication = function() {
		router.route("account/interestDelayApplication.html?financeId=" + financeId);
	}

	//延期还款
	var delayPickypApplication = function() {
		router.route("account/accountPickupDelayApplication.html?financeId=" + financeId);
	}

	//延期还款
	var detainedApplication = function() {
		router.route("account/accountDetainedApplication.html?financeId=" + financeId);
	}

	//货物一览
	var bulkstockListing = function() {
		router.route("account/accountBulkstockListing.html?financeId=" + financeId + "&productType=" + productType);
	}
	
	//合同一览
	var documentSign = function() {
		router.route("account/documentSign.html?financeId=" + financeId+"&financeState="+financeState);
	}

	//额度处理
	var verifyFinanceApplication = function() {
		$("#popdialog").html(html_verify);
		dialog.showDialog($("#applyVerify"));
		financeNo = document.getElementById('financeNo').innerHTML;
		financeAmountt = document.getElementById('financeAmountt').innerHTML;
		$(".financeNumber").html(financeNo);
		$(".financeAmount").html("￥" + financeAmountt);

		databind.fillInput($("#applyVerify"), {
			action: "inquireCurrentEnterpriseDetail.json",
			version: "v1.0"
		});
		$("#verifySure").unbind("click").click(function() {
			consentFinanceAuditAmountApplication();
		});

		$("#verifyCancel").unbind("click").click(function() {
			cancelFinanceApplication();
		});
	}

	//接受融资审批金额
	var consentFinanceAuditAmountApplication = function() {
		base.fnAjax({
			action: "consentFinanceAuditAmountApplication.json",
			async: false,
			oData: {
				financeId: financeId
			},
			fnSuccess: function(data) {
				tools.cancelProtectDoubleClick();
				if (data.status == 200) {
					base.alert(1, "操作成功！");
					dialog.hideDialog($("#applyVerify"));
					router.route("account/accountMain.html");
				} else {
					base.alert(2, "数据获取失败");
				}
			}
		});
	};
	
	//取消融资申请
	var cancelFinanceApplication = function() {
		base.fnAjax({
			action: "cancelFinanceApplication.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "操作成功！");
					dialog.hideDialog($("#applyVerify"));
					router.route("account/accountMain.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	//查询产品详情
	var inquireFinanceProductDetail = function(productId) {
		var bailRate = 0;
		base.fnAjax({
			action: "inquireFinanceProductDetail.json",
			async: false,
			oData: {
				productId: productId
			},
			fnSuccess: function(data) {
				if (data.status == 200) {
					bailRate = data.datas[0].bailRate;
				} else {
					base.alert(2, "数据获取失败");
				}
			}
		});
		return bailRate;
	};
	
	var showEvaluteDetail = function(){
		router.route("account/accountEvaluateDetail.html?financeId="+financeId);
	}

	//查询融资信息详情
	var inquireFinanceApplicationDetail = function() {
		base.fnAjax({
			action: "inquireFinanceApplicationDetail.json",
			async: false,
			oData: {
				financeId: financeId

			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("formatCurrency", tools.formatCurrency);
					juicer.register("getValueByKey", constant.getValueByKey);
					if (data.datas[0].productId != null && data.datas[0].productId != 0) {
						data.datas[0].bailRate = inquireFinanceProductDetail(data.datas[0].productId);
					}
					productType = data.datas[0].productType;
					var tpl = $("#accountDetailTpl").html();
					var html = juicer(tpl, data);
					$("#accountDetailBody").html(html);
					financeState = $("#financeState").val();
					productId = data.datas[0].productId;
					enterpriseId = data.datas[0].enterpriseId;
					productType=data.datas[0].productType;
					//					financeState = 10;
					enterpriseName = data.datas[0].enterpriseName;
					financeNumber = data.datas[0].financeNumber;

					if(productType == 2){
						
						if (financeState == 1) {
							
						} else if (financeState == 2) {
							$(".buttonOperate").html("额度处理").attr("id", "verifyApply").removeClass("none");
							$(".buttonOperate:first").before('<a class="buttonOperate" id="cancelApply">取消申请</a>');
						} else if (financeState == 4) {
							$(".buttonOperate").html("修改").attr("id", "modifyApply").removeClass("none");
							$(".buttonOperate:first").before('<a class="buttonOperate" id="cancelApply">取消申请</a>');
						} else if (financeState == 17) {
							inquireFinanceEvaluate();
						} else if (financeState == 18) {
							$(".buttonOperate").html("评价").attr("id", "evalute").removeClass("none");
						} else if (financeState == 19) {
							$(".buttonOperate").html("评价信息").attr("id", "evaluteDetail").removeClass("none");
						} else if (financeState == 20) {
							$(".buttonOperate").html("取消申请").attr("id", "cancelApply").removeClass("none");
							$(".buttonOperate:first").before('<a class="buttonOperate" id="modifyApply">修改</a>');
							$(".buttonOperate:first").before('<a class="buttonOperate" id="submitApply">提交融资</a>');
						} else {
							$(".buttonOperate").hide();
						}
						
						if (data.datas[0].bail - data.datas[0].bailAlreadyReturn > 0) {
							$(".buttonOperate:first").before('<a class="buttonOperate" id="bailReturn">退还保证金</a>');
						}
						if (financeState > 5) {
							$(".buttonOperate:first").before('<a class="buttonOperate" id="bulkstockListing">货物一览</a>');
						}
						if (financeState == 24) {
							$(".buttonOperate:first").before('<a class="buttonOperate" id="documentSign">合同签署</a>');
						}
						if (financeState >= 6) {
							$(".exceptionOperate").removeClass("none");
							$(".buttonOperate:first").before('<a class="buttonOperate" id="pickupApply">申请提货</a>');
						}
					}else if(productType == 6){
						if (data.datas[0].bail - data.datas[0].bailAlreadyReturn > 0) {
							$(".buttonOperate:first").before('<a class="buttonOperate" id="bailReturn">退还保证金</a>');
						}
						if (financeState > 5) {
							$(".buttonOperate:first").before('<a class="buttonOperate" id="bulkstockListing">货物一览</a>');
						}	
						if (financeState == 24 || financeState == 23 || financeState == 22) {
							$(".buttonOperate:first").before('<a class="buttonOperate" id="documentSign">合同签署</a>');
						}
	
						$(".buttonOperate:first").before('<a class="buttonOperate" id="createPickup11_s2">提货</a>');
					}else if(productType == 5){
						if (financeState > 5) {
							$(".buttonOperate:first").before('<a class="buttonOperate" id="bulkstockListing">货物一览</a>');
						}
						if (financeState == 24 || financeState == 23 || financeState == 22) {
							$(".buttonOperate:first").before('<a class="buttonOperate" id="documentSign">合同签署</a>');
						}
						$(".buttonOperate:first").before('<a class="buttonOperate" id="createPickup11_s2">提货</a>');
					}
					
					$(".buttonOperate:first").before('<a class="buttonOperate" id="calcelApply">融资申请取消</a>');
//					if(productType == 6){
//						$("#pickupApply").hide();
//						$("#createPickup11").hide();
//					}
//					if(productType == 5 || productType == 6){
//						$("#pickupApply").hide();
//					}
					if(productType == 2){
						$("#createPickup11").hide();
					}
					$("#calcelApply").unbind("click").click(function(){
						var ii = layer.open({
						  title: '融资取消申请',
						  type: 1,
						  skin: 'layui-layer-rim', //加上边框
						  area: ['800px', '300px'], //宽高
						  content: html_cancelApplication,
						  btn: ['确定', '取消'],
						  yes: function(){
						  	databind.submitDom($(".createFinanceCancelApplication"), function(){
						  		layer.msg("申请成功！");
						  		layer.close(ii);
						  		router.route("account/accountDetail.html?financeId=" + financeId);
						  	});
						  }
						});
						
						$(".createFinanceCancelApplication input[name=financeId]").val(financeId);
					});

					//取消融资
					$("#cancelApply").unbind("click").click(function() {
						dialog.showConfirmDialog("取消融资", "是否确定取消融资？", function(){
							cancelFinanceApplication();
						});
					});
					
					//提交融资
					$("#submitApply").unbind("click").click(function() {
						dialog.showConfirmDialog("提交融资", "提交以后不能再次修改,是否确定提交融资？", function(){
							submitFinanceApplication();
						});
					});

					//修改融资
					$("#modifyApply").unbind("click").click(function() {
						updateFinanceApplication();
					});

					//提货申请
					$("#pickupApply").unbind("click").click(function() {
						pickupFinanceApplication();
					});

					//评价
					$("#evalute").unbind("click").click(function() {
						evaluteFinanceApplication();
					});

					$("#verifyApply").unbind("click").click(function() {
						verifyFinanceApplication();
					});

					//赎货申请
					$("#ransomPickupBtn").unbind("click").click(function() {
						ransomApplication();
					});

					//赎货申请
					$("#prePickupBtn").unbind("click").click(function() {
						preApplication();
					});

					//延期计息
					$("#interestDelayBtn").unbind("click").click(function() {
						interestDelayApplication();
					});

					//延期还款
					$("#delayPickupBtn").unbind("click").click(function() {
						delayPickypApplication();
					});

					//扣货申请
					$("#detainedApplication").unbind("click").click(function() {
						detainedApplication();
					});

					//货物一览
					$("#bulkstockListing").unbind("click").click(function() {
						bulkstockListing();
					});
					
					$("#accountDetail .productDetail").unbind("click").click(function(){
						window.open("../product/productDetail.html?productId=" + $(this).attr("productId") + "&financeState=" + financeState);
					});
					
					//合同一览
					$("#documentSign").unbind("click").click(function() {
						documentSign();
					});
					
					//保证金退还
					$("#bailReturn").unbind("click").click(function() {
						var ii = layer.open({
						  title: '保证金退还',
						  type: 1,
						  skin: 'layui-layer-rim', //加上边框
						  area: ['300px', '150px'], //宽高
						  content: html_bailReturn,
						  btn: ['确定', '取消'],
						  yes: function(){
						  	databind.submitDom($(".createBailReturn"), function(){
						  		layer.msg("申请成功！");
						  		layer.close(ii);
						  	});
						  }
						});
						$(".createBailReturn input[name=financeId]").val(financeId);
						tools.clearNoNumAll($(".createBailReturn"));
					});
					//提货
					$("#createPickup11_s2").unbind("click").click(function(){
						if(productType == 5){
							router.route("account/createPickup6.html?enterpriseId=" + enterpriseId +"&financeNumber=" + financeNumber+"&enterpriseName="+enterpriseName+"&productType="+productType+"&financeId="+financeId);
						}else if(productType == 6){
							router.route("account/createPickup4.html?enterpriseId=" + enterpriseId +"&financeNumber=" + financeNumber+"&enterpriseName="+enterpriseName+"&productType="+productType+"&financeId="+financeId);
						}
						
					});
//					$("#createPickup11").unbind("click").click(function(){
//						if(productType == 5){
//							router.route("account/createPickup.html?enterpriseId=" + enterpriseId +"&financeNumber=" + financeNumber+"&enterpriseName="+enterpriseName+"&productType="+productType);
//						}else if(productType == 6){
//							router.route("account/createPickup3.html?enterpriseId=" + enterpriseId +"&financeNumber=" + financeNumber+"&enterpriseName="+enterpriseName+"&productType="+productType);
//						}
//						
//					});
					//查看评价信息
					$("#evaluteDetail").unbind("click").click(function() {
						showEvaluteDetail();
					});
					
					if (data.datas[0].financeConstractSample == "") {
						$("#accountDetail .constractSample").parent().remove();
					}
					
					//查看合同范本
					$("#accountDetail .constractSample").unbind("click").click(function() {
						$("#popdialog").html(html_showSample);
						var files = {
							fileUuids: []
						};
						
						
						$.each(data.datas[0].financeConstractSample.split(','), function(){
							if (this != "") {
								files.fileUuids.push(this);
							}
						});
						juicer.register("inquireFile", tools.inquireFile);
						
						var tpl = $("#constractSample #sampleTpl").html();
						var html = juicer(tpl, files);
						$("#constractSample #sampleTable").html(html);
						
						dialog.showDialog($("#constractSample"));
						
						$("#constractSample .sure_yes").unbind("click").click(function(){
							dialog.hideDialog($("#constractSample"));
						});
					});
					
					$(".showcoldStorage").unbind("click").click(function(){
						var $this = $(this);
						var ds = [];
						var d = {};
						d.precool = $this.attr("precool");
						d.ptiOk = $this.attr("ptiOk");
						d.plug = $this.attr("plug");
						d.plugType = $this.attr("plugType");
						d.tempUpDown = $this.attr("tempUpDown");
						d.setTemperature = $this.attr("setTemperature");
						d.tempUnit = $this.attr("tempUnit");
						d.tempMax = $this.attr("tempMax");
						d.tempMin = $this.attr("tempMin");
						d.vent = $this.attr("vent");
						d.coolLetter = $this.attr("coolLetter");
						d.coolRemark = $this.attr("coolRemark");
						ds.push(d);
						data.ds = ds;
						$("#popdialog").html(html_coldStorage);
						$("#coldStorage_dialog .dialogTitle").html("冷藏品信息");
						juicer.register("getValueByKey", constant.getValueByKey);
						var tpl = $("#coldStorageTpl").html();
						var html = juicer(tpl, data);
						$("#coldStorageBody").html(html);
						dialog.showDialog($("#coldStorage_dialog"));
						
						$("#coldStorage_dialog .sure_yes").unbind("click").click(function(){
							dialog.hideDialog($("#coldStorage_dialog"));
						});
					});
					
					$(".showDangerous").unbind("click").click(function(){
						var $this = $(this);
						var ds = [];
						var d = {};
						d.declaration = $this.attr("declaration");
						d.instructionManual = $this.attr("instructionManual");
						d.dangerousRemark = $this.attr("dangerousRemark");
						d.unNum = $this.attr("unNum");
						d.imdg = $this.attr("imdg");
						ds.push(d);
						data.ds1 = ds;
						$("#popdialog").html(html_coldStorage);
						$("#coldStorage_dialog .dialogTitle").html("危险品信息");
						juicer.register("getValueByKey", constant.getValueByKey);
						var tpl = $("#dangerousTpl").html();
						var html = juicer(tpl, data);
						$("#coldStorageBody").html(html);
						dialog.showDialog($("#coldStorage_dialog"));
						
						$("#coldStorage_dialog .sure_yes").unbind("click").click(function(){
							dialog.hideDialog($("#coldStorage_dialog"));
						});
					});
					
					$.each($("#accountDetail table"), function() {
						var $this = $(this);
						if($this.find("tr").length==1){
							$this.remove();
						}
					});
					
					if (type == "readonly") {
						$(".buttonOperate").remove();
						$(".exceptionOperate").remove();
					}
				} else {}
			}
		});
	};

	return {
		init: init
	};
});